package com.example.day2_menu;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

/***
 * 上下文菜单：ContextMenu
 * 1。创建menu资源文件
 * 2。activity重写onCreateContextMenu
 * 3。activity重写onContextItemSelected
 *
 * 弹出菜单：PopupMenu
 * 1：创建PopupMenu对象
 * 2：加载布局：inflate（）
 * 3：设置监听：setOnMenuItemClickListener（）
 * 4：显示
 */
public class Main2Activity extends AppCompatActivity {
    TextView tv_context,tv_popup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        tv_context = findViewById(R.id.tv_context);
        tv_popup = findViewById(R.id.tv_popup);
        //*****上下文菜单和指定控件绑定
        registerForContextMenu(tv_context);
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.menu_context,menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }
    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.item1:
                Toast.makeText(this, "111", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item2:
                Toast.makeText(this, "222", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item3:
                Toast.makeText(this, "333", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }
}
